package com.saile.controller.admin.news;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saile.controller.base.BaseController;
import com.saile.def.Constant;
import com.saile.domain.News;
import com.saile.result.ResultObj;
import com.saile.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-15.
 */
@RequestMapping("/admin/news")
@Controller
public class AdminNewsController extends BaseController {
    @Autowired
    NewsService newsService;

    /**
     * 获取新闻列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/getNewsList")
    public ModelAndView getNewsList(Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/news/getNewsList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageHelper.startPage(page, pageSize);
        List<News> newsList = newsService.getNewsList(map);
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        m.addObject("pageInfo", pageInfo);
        m.addObject("bean", map);
        return m;
    }

    /**
     * 编辑资源提交
     *
     * @return
     */
    @RequestMapping(value = "/news")
    public ModelAndView news(Long tid) {
        ModelAndView m = new ModelAndView("/admin/news/news");
        if (tid!=null) {
            Map<String, String> map = new HashMap<>();
            map.put("tid",String.valueOf(tid));
            News news = newsService.getNews(map);
            m.addObject("news", news);
        }

        return m;
    }


    /**
     * 更新新闻
     *
     * @param news
     * @return
     */
    @RequestMapping(value = "/modifyNews")
    @ResponseBody
    public String modifyNews(News news, MultipartFile location) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        String newPicUrl = "";
        try {

            try {
                String filePath = upLoadFile(location);
                newPicUrl = filePath;
                logger.info("resourceUrl=============>>>" + newPicUrl);
                news.setNewsTitlePic(newPicUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }

            newsService.modifyNews(news);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    /**
     * 删除新闻
     *
     * @param newsId
     * @return
     */
    @RequestMapping(value = "/deleteNews")
    @ResponseBody
    public String deleteNews(Long newsId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        String newPicUrl = "";
        try {

            newsService.deleteNews(newsId);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
